package com.xxxx.server.controller;

import com.xxxx.server.pojo.Admin;
import com.xxxx.server.pojo.AdminLoginParam;
import com.xxxx.server.pojo.RespBean;
import com.xxxx.server.service.IAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import net.minidev.json.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;


//用在控制层
@Api(tags = "LoginController1")
@RestController
public class LoginController {
    @Autowired
    private IAdminService adminService;

    @ApiOperation(value="登录之后返回token")
    @PostMapping("/login")
    public RespBean login(@RequestBody AdminLoginParam adminLoginParam, HttpServletRequest request){
        return adminService.login(adminLoginParam.getCode(),adminLoginParam.getUsername(),adminLoginParam.getPassword(),request);
    }

    @ApiOperation(value="获取登录用户信息")
    @GetMapping("/admin/info")
    public Admin getAdminInfo(Principal principal){
            if (null==principal){
                return null;
            }
            String username=principal.getName();
            Admin admin=adminService.getAdminbyUserName(username);
            admin.setRoles(adminService.getRoles(admin.getId()));
            admin.setPassword(null);
            return admin;
    }


    @ApiOperation(value="退出登录")
    @PostMapping("/logout")
    public RespBean logout(){
        return RespBean.success("注销成功");
    }
}
